package com.hangzhou.welbeing.welbeinginstrument.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hangzhou.welbeing.welbeinginstrument.R;
import com.hangzhou.welbeing.welbeinginstrument.base.MyApplication;
import com.hangzhou.welbeing.welbeinginstrument.bean.LoginBean;
import com.hangzhou.welbeing.welbeinginstrument.config.RBConstants;
import com.hangzhou.welbeing.welbeinginstrument.tool.PhoneFormatCheckUtils;
import com.hangzhou.welbeing.welbeinginstrument.utils.Utils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.Callback;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * 时间 2017 年 08 月 21.
 * 作者 Administrator  刘传华
 * 公司 杭州惟君医疗科技有限公司
 * 包名 com.hangzhou.welbeing.pregnant.welbeing_pregnant.activity
 * 工程名 welbeing_pregnant
 *
 * @desc 登录页面
 */
public class LoginActivity extends Activity {


    private static final String TAG ="LoginActivity" ;
    @Bind(R.id.activity_login_iv_return)
    ImageView activityLoginIvReturn;
    @Bind(R.id.activity_login_rl_title)
    RelativeLayout activityLoginRlTitle;
    @Bind(R.id.activity_login_et_phone_number)
    EditText activityLoginEtPhoneNumber;
    @Bind(R.id.activity_login_et_password)
    EditText activityLoginEtPassword;
    @Bind(R.id.activity_login_tv_forget_password)
    TextView activityLoginTvForgetPassword;
    @Bind(R.id.activity_login_ll_input)
    LinearLayout activityLoginLlInput;
    @Bind(R.id.activity_login_rl_login)
    RelativeLayout activityLoginRlLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);


        Utils.setStatusBar(this,false,false);
        Utils.setStatusTextColor(true,this);

        //手机号
        activityLoginEtPhoneNumber.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String PhoneNumber = charSequence.toString().trim();
                if(PhoneNumber.length()>10){
                    if(PhoneFormatCheckUtils.isPhoneLegal(PhoneNumber)){
                        Toast.makeText(LoginActivity.this,"手机号格式正确",Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(LoginActivity.this,"对不起您输入的不是手机号，请重新输入",Toast.LENGTH_LONG).show();
                        activityLoginEtPhoneNumber.setTextColor(0xffFF0000);
                    }
                }

            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });

        SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
        final String login_password = sp.getString("login_password","");
        //密码
        activityLoginEtPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String password = charSequence.toString().trim();
                if(password.length() == login_password.length()){
                    if(password.equals(login_password)){
                        Toast.makeText(LoginActivity.this,"密码正确",Toast.LENGTH_LONG).show();
                    }else{
                        Toast.makeText(LoginActivity.this,"密码错误",Toast.LENGTH_LONG).show();
                        activityLoginEtPassword.setTextColor(0xffFF0000);
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @OnClick({R.id.activity_login_iv_return, R.id.activity_login_tv_forget_password, R.id.activity_login_rl_login})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.activity_login_iv_return:
                Intent intent2 = new Intent(LoginActivity.this,EntranceActivity.class);
                startActivity(intent2);
                finish();
                break;
            case R.id.activity_login_tv_forget_password:
                Intent intent = new Intent(LoginActivity.this,ForgetPasswordActivity.class);
                startActivity(intent);
                //finish();
                break;
            case R.id.activity_login_rl_login:
                String PhoneNumber = activityLoginEtPhoneNumber.getText().toString().trim();
                String Password = activityLoginEtPassword.getText().toString().trim();
                if(!TextUtils.isEmpty(PhoneNumber)){
                    if(!TextUtils.isEmpty(Password)){
                        login(PhoneNumber,Password);
                    }else{
                        Toast.makeText(LoginActivity.this,"请输入密码",Toast.LENGTH_LONG).show();
                    }
                }else{
                    Toast.makeText(LoginActivity.this,"请输入手机号",Toast.LENGTH_LONG).show();
                }
                break;
        }
    }
    /**
     * @desc 登录
     * @param phoneNumber
     * @param password
     * */
    private void login(String phoneNumber, String password) {
        OkHttpUtils
                .post()
                .url(RBConstants.SERVER_URL+RBConstants.LOGIN)
                .addParams("tel", phoneNumber)
                .addParams("password", password)
                .build()
                .execute(new Callback() {
                    @Override
                    public Object parseNetworkResponse(Response response, int id) throws Exception {
                        String parseNetworkResponse = response.body().string();
                        //Log.e(TAG,"parseNetworkResponse========"+parseNetworkResponse);
                       Gson gson =  new Gson();
                        final LoginBean loginBean =  gson.fromJson(parseNetworkResponse,LoginBean.class);
                            MyApplication.getmHandler().post(new Runnable() {
                                @Override
                                public void run() {
                                    if(loginBean.errorCode == 200){
                                        Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_LONG).show();
                                        SharedPreferences sp = getSharedPreferences("second_app_internal_data", 0);
                                        SharedPreferences.Editor edit = sp.edit();
                                        edit.putString("user_id",loginBean.data.id); //loginBean.data.id
                                        edit.putString("cus_logo",loginBean.data.cus_logo); //loginBean.data.id
                                        edit.putString("cus_name",loginBean.data.cus_name); //loginBean.data.id
                                        edit.putString("act",loginBean.data.act); //loginBean.data.id
                                        edit.putString("signature",loginBean.data.signature); //loginBean.data.id
                                        edit.putString("user_set_due_date",loginBean.data.birthday); //预产期
                                        edit.putString("tel",loginBean.data.tel); //预产期
                                        edit.putString("token",loginBean.data.token);
                                        edit.putBoolean("is_login",true);
                                        edit.commit();


                                        Intent intent2 = new Intent(LoginActivity.this,MainActivity.class);
                                        startActivity(intent2);
                                        finish();
                                        overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
                                    }else if(loginBean.errorCode == 404){
                                        Toast.makeText(LoginActivity.this,"404",Toast.LENGTH_LONG).show();
                                    }else{
                                        Toast.makeText(LoginActivity.this,loginBean.errorMessage,Toast.LENGTH_LONG).show();
                                    }
                                }
                            });
                        return null;
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {

                    }

                    @Override
                    public void onResponse(Object response, int id) {

                    }
                });

    }
}
